package ru.timofeyco.justfeelbetter.adapters;

import java.util.Calendar;

import ru.timofeyco.justfeelbetter.R;
import ru.timofeyco.justfeelbetter.model.DayResult;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MonthAdapter extends BaseAdapter {
	private Context context;

	DayResult[] monthResult;
	
	public MonthAdapter(Context context, int cols, int rows, DayResult[] monthResult) {
		this.context = context;

		this.monthResult = monthResult;
	}

	@Override
	public int getCount() {		
		return monthResult.length;
	}

	@Override
	public Object getItem(int position) {		
		return true;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (position >= monthResult.length) {
			return null;
		}
		DayResult dayResult = monthResult[position];
		int color = Color.GRAY;
		if (dayResult != null) {
			if (dayResult.getProductivityPercent() >= 100) {
				color = Color.GREEN;
			} else if (dayResult.getProductivityPercent() == 0) {
				color = Color.RED;
			} else {
				color = Color.YELLOW;
			}
		}
        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   	 	LinearLayout linearLayoutView = (LinearLayout)layoutInflater.inflate(R.layout.cell_day, null);
   	 	linearLayoutView.setBackgroundColor(color);
   	 	linearLayoutView.setPadding(6, 6, 6, 6);

   	 	TextView textView = (TextView) linearLayoutView.findViewById(R.id.dayTextView);   	 	
   	 	textView.setText(Integer.toString(position + 1));

   	 	if ((position + 1) == Calendar.getInstance().getTime().getDate()) {

   	 		textView.setTypeface(Typeface.DEFAULT_BOLD);
   	 	}
   	 	
        
        return linearLayoutView;
	}
	
}
